/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.unsw.bookmarket.action;

import au.edu.unsw.bookmarket.actionform.BookBean;
import au.edu.unsw.bookmarket.actionform.CourseBean;
import au.edu.unsw.bookmarket.actionform.PostAdsBean;
import au.edu.unsw.bookmarket.actionform.SellerBean;
import au.edu.unsw.bookmarket.service.BookService;
import au.edu.unsw.bookmarket.service.CourseService;
import au.edu.unsw.bookmarket.service.SellerService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author gongz
 */
public class PostAdsAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    BookService bookService;
    CourseService courseService;
    SellerService sellerService;

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        BookBean bookBean = null;
        CourseBean courseBean = null;
        PostAdsBean postAdsBean = (PostAdsBean) form;
        if (postAdsBean.getItem_type().equals("book")) {
            bookBean = bookService.addBook(postAdsBean);
            courseBean = courseService.addCourse(postAdsBean);
        }
        sellerService.addSeller(postAdsBean);
        List<BookBean> books = bookService.getAllBooks();
        List<CourseBean> courses = courseService.getAllCourses();
        List<SellerBean> sellers = sellerService.getAllSellers();
        System.out.println("there're " + books.size() + " books in DB:");
        for (BookBean book : books) {
            System.out.println(book.getName());
            System.out.println(book.getIsbn());
        }

        System.out.println("there're " + courses.size() + " courses in DB:");
        for(CourseBean course : courses){
            System.out.println(course.getCode());
            System.out.println(course.getName());
        }
        System.out.println("there're " + sellers.size() + " sellers in DB:");
           for(SellerBean seller : sellers){
            System.out.println(seller.getUsername());
        }
        if (bookBean != null) {
            System.out.println("the book has already in DB:");
            System.out.println(bookBean.getName());
        }
        if (courseBean != null) {
            System.out.println("the course has already in DB:");
            System.out.println(courseBean.getCode());
        }
        request.setAttribute("books", books);
        request.setAttribute("courses", courses);
        request.setAttribute("sellers", sellers);
        return mapping.findForward(SUCCESS);
    }
}
